package com.zhoug.chart3demo.chart;

import android.graphics.Color;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;

import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.RadarDataSet;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.zhoug.chart3.MPChartColors;
import com.zhoug.chart3.charts.MPRadarChart;
import com.zhoug.chart3.core.styleconfig.IMPRadarStyleConfig;
import com.zhoug.chart3.data.HkChartData;
import com.zhoug.chart3.data.OnDateSetCreateListener;
import com.zhoug.chart3.markers.MPRadarMarker;
import com.zhoug.chart3demo.BaseActivity;
import com.zhoug.chart3demo.DataGenerator;
import com.zhoug.chart3demo.R;
import com.zhoug.common3.http.SchedulerUtils;
import com.zhoug.common3.utils.TextUtils;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.disposables.Disposable;


/**
 * 雷达图
 *
 * @Author: zhoug
 * @Date: 2024-02-07
 * @Description:
 */
public class RadarChartActivity extends BaseActivity {
    private static final String TAG = ">>>RadarChartActivity";
    private static final int mRadarColor=Color.RED;
    private Button mBtnSetting;
    private MPRadarChart mRadarChart1;
    private MPRadarChart mRadarChart2;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private Switch mSwitchFill;
    private EditText mEtFillAlpha;
    private Switch mSwitchDrawLine;
    private Switch mSwitchWebFill;
    private EditText mEtWebFillColor;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_radarchart;
    }

    @Override
    protected void findViews() {

        mBtnSetting = findViewById(R.id.btn_setting);
        mRadarChart1 = findViewById(R.id.radarChart1);
        mRadarChart2 = findViewById(R.id.radarChart2);
        mSwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
        mSwitchFill = findViewById(R.id.switchFill);
        mEtFillAlpha = findViewById(R.id.et_fillAlpha);
        mSwitchDrawLine = findViewById(R.id.switchDrawLine);
        mSwitchWebFill = findViewById(R.id.switchWebFill);
        mEtWebFillColor = findViewById(R.id.et_webFillColor);
    }

    @Override
    protected void init() {
        MPRadarMarker radarMarker=new MPRadarMarker(this);
        radarMarker.showSum("违规总数",true);
        radarMarker.setShowType(MPRadarMarker.SHOW_TYPE_All);
        radarMarker.showPercent(true);
        mRadarChart1.setMarker(radarMarker);
        radarMarker=new MPRadarMarker(this);
        radarMarker.setShowType(MPRadarMarker.SHOW_TYPE_HIGHLIGHT);
        mRadarChart2.setMarker(radarMarker);
        mRadarChart1.setOnDateSetCreateListener(new OnDateSetCreateListener<RadarDataSet>() {
            @Override
            public void onInitDataSetStyle(RadarDataSet dataSet, int index) {
                dataSet.setDrawValues(true);
            }
        });
        mRadarChart2.setOnDateSetCreateListener(new OnDateSetCreateListener<RadarDataSet>() {
            @Override
            public void onInitDataSetStyle(RadarDataSet dataSet, int index) {
                dataSet.setDrawValues(true);
            }
        });
        mSwipeRefreshLayout.setOnRefreshListener(this::loadData);
        mSwitchFill.setOnCheckedChangeListener(onCheckedChangeListener);
        mSwitchDrawLine.setOnCheckedChangeListener(onCheckedChangeListener);
        mSwitchWebFill.setOnCheckedChangeListener(onCheckedChangeListener);
        mBtnSetting.setOnClickListener(v->{
            setting();
            loadData();
        });
        YAxis yAxis = mRadarChart1.getYAxis();
        yAxis.setAxisMaximum(100);
        yAxis = mRadarChart2.getYAxis();
        yAxis.setAxisMaximum(10);

        setting();
        loadData();
    }

    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener = (buttonView, isChecked) -> {
        setting();
        loadData();
    };

    private void setting() {
        boolean isFill = mSwitchFill.isChecked();
        boolean drawLine = mSwitchDrawLine.isChecked();
        boolean drawWebFill = mSwitchWebFill.isChecked();
        String s = mEtWebFillColor.getText().toString();
        try {
            int webFillColor = Color.parseColor(s);
            mRadarChart1.setDrawWebFill(drawWebFill);
            mRadarChart1.setWebFillColor(webFillColor);
            mRadarChart2.setDrawWebFill(drawWebFill);
            mRadarChart2.setWebFillColor(webFillColor);
        } catch (Exception e) {
            e.printStackTrace();
            toast("无效的颜色");
        }

        int fillAlpha = TextUtils.parseInt(mEtFillAlpha.getText().toString(), 88);
        if(fillAlpha<0){
            fillAlpha=0;
        }else if(fillAlpha>255){
            fillAlpha=255;
        }
        IMPRadarStyleConfig styleConfig = mRadarChart1.getStyleConfig();
        styleConfig.setRadarColor(mRadarColor);
        styleConfig.setFillAlpha(fillAlpha);
        if(isFill){
            styleConfig.setFillColors(new int[]{mRadarColor});
        }else{
            styleConfig.setFillColors(null);
        }
        styleConfig = mRadarChart2.getStyleConfig();
        styleConfig.setRadarColor(mRadarColor);
        styleConfig.setFillAlpha(fillAlpha);
        if(isFill){
            styleConfig.setFillColors(new int[]{mRadarColor, MPChartColors.COLOR_BLUE, MPChartColors.COLOR_PURPLE});
        }else{
            styleConfig.setFillColors(null);
        }
        mRadarChart1.setDrawLine(drawLine);
        mRadarChart2.setDrawLine(drawLine);



    }

    private void loadData() {
        Disposable subscribe = Observable
                .create((ObservableOnSubscribe<List< List<HkChartData>>>) emitter -> {
                    Thread.sleep(200);
                    Random random = new SecureRandom();
                    List< List<HkChartData>> lists=new ArrayList<>();
                    List<HkChartData> list = new ArrayList<>();
                    lists.add(list);
                    String[] aiIllegal = DataGenerator.AI_ILLEGAL;
                    for (int i = 0; i < aiIllegal.length; i++) {
                        HkChartData chartData = new HkChartData();
                        chartData.setName(aiIllegal[i]);
                        chartData.put("数量", random.nextInt(100));
                        list.add(chartData);
                    }
                   list = new ArrayList<>();
                    lists.add(list);
                    for (int i = 0; i < aiIllegal.length; i++) {
                        HkChartData chartData = new HkChartData();
                        chartData.setName(aiIllegal[i]);
                        chartData.put("数量", random.nextInt(10));
                        list.add(chartData);
                    }
                    emitter.onNext(lists);
                    emitter.onComplete();

                })
                .compose(SchedulerUtils.apply())
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this)))
                .subscribe(data -> {
                    mSwipeRefreshLayout.setRefreshing(false);
                    mRadarChart1.setHkChartData(data.get(0));
                    mRadarChart2.setHkChartData(data.get(1));


                }, throwable -> {
                    mSwipeRefreshLayout.setRefreshing(false);
                    throwable.printStackTrace();
                    mRadarChart1.error();
                    mRadarChart2.error();
                });


    }


}